import 'package:flutter/material.dart';
import '../../../common/utils/shortcut.dart';
import '../../ui.dart';

class MyTextFieldDialog extends StatelessWidget {
  final String? title;
  final String? desc;
  final String? initText;
  final String? hintText;
  final String? confirmButtonText;
  final String? cancelText;
  final bool obscureText;
  final TextInputType? keyboardType; //键盘类型

  const MyTextFieldDialog({
    super.key,
    this.initText,
    this.hintText,
    this.title,
    this.confirmButtonText,
    this.cancelText,
    this.obscureText = false,
    this.keyboardType,
    this.desc,
  });

  Future<String?> show({bool barrierDismissible = true}) {
    return openDialog(this, barrierDismissible: barrierDismissible);
  }

  @override
  Widget build(BuildContext context) {
    final textEditingController = TextEditingController(text: initText);

    return MyDialog(
      title: title,
      contentWidget: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const SizedBox(
            height: 4,
          ),
          desc != null
              ? MyText(
                  desc!,
                  size: 13,
                  textAlign: TextAlign.center,
                )
              : const SizedBox.shrink(),
          const SizedBox(
            height: 12,
          ),
          MyTextField(
            controller: textEditingController,
            hintText: hintText,
            autofocus: true,
            obscureText: obscureText,
            keyboardType: keyboardType,
            onSubmitted: (text) {
              back(result: textEditingController.text);
            },
          ),
        ],
      ),
      positionTopCenter: true,
      actions: [
        MyDialogAction(
            text: cancelText ?? "取消",
            bold: true,
            onTap: () {
              back();
            }),
        MyDialogAction(
            text: confirmButtonText ?? "确定",
            onTap: () {
              back(result: textEditingController.text);
            }),
      ],
    );
  }
}
